package com.thread;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveAction;

public class RecursiveActionDemo {

    public static void main(String[] args) throws InterruptedException {
        ForkJoinPool pool = new ForkJoinPool();
        int[] data = {1,2,3,4,5,6,7,8,9,10};
        Square app = new Square(data,0,data.length);
        pool.submit(app);
        System.out.println(app.result);
    }

}
class Square extends RecursiveAction {

    final int LIMIT = 3;
    static int result;
    int start,end;
    int[] data;

    public Square(int[] data,int start,int end) {
        this.start = start;
        this.end = end;
        this.data = data;
    }

    @Override
    public void compute() {
        if ((end - start) < LIMIT) {
            for (int i=start;i<end;i++){
                result+=data[i]*data[i];
            }
        }else {
            int mid = (start + end)/2;
            Square left = new Square(data,start,mid);
            Square right = new Square(data,mid,end);
            left.fork();
            right.fork();
            left.join();
            right.join();

        }
    }


}
